*****************************************************************************************************************************************************
* PROJECT: 		WEALTH LOSSES DUE TO CORONA PANDEMIC
* DATE: 		2020-November
* CONTENT: 		Table A12
* AUTHORS: 		Tobin Hanspal, Johannes Wohlfart, Anni Weber
*****************************************************************************************************************************************************

		clear all
		set scheme lean2
		global pm = char(177)
		set more off
		gl date = c(current_date)
		graph set window fontface "cmr10"		
		
* set cd as /Replication_files_HanspalWeberWohlfart2020/

*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~						
** Table A12
*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~	
use data/processed/data_1, clear

	* Scale USD variables	
		foreach x in  delofin_USD_tr02 delretir_USD_tr02  delfinwealth_USD_tr02   delhhnetinc_USD_tr02 ///
		delofin_USD_tr02alt delretir_USD_tr02alt  delfinwealth_USD_tr02alt	{
			replace `x' = `x'/1000
		}
		foreach x in ofinwealth_lvl retirewealth_lvl  finwealth_Jan20 hhinc_lvl hhnetinc_2019   {
			replace `x' = `x'/1000
		}
	*
	drop if agecat==.
	*baseline for age is 35-44 (agecat==3)
	
	preserve
		collapse (mean) del_ofin_perc_tr02 del_retir_perc_tr02  del_finwealth_perc_tr02, by(agecat) 
			foreach x in del_ofin_perc_tr02 del_retir_perc_tr02 del_finwealth_perc_tr02 {
				gen temp_`x' = `x' if agecat==3
				egen `x'M = max(temp_`x')
				drop temp*
				gen dif_`x' = `x'M-`x'
			}
			keep dif* agecat
			gen uncond = 1
		save temp/uncond_agecat_2, replace
	restore	

	preserve
		keep if finwealth_Jan20>0
		collapse (mean) del_ofin_perc_tr02 del_retir_perc_tr02  del_finwealth_perc_tr02, by(agecat) 
			foreach x in del_ofin_perc_tr02 del_retir_perc_tr02 del_finwealth_perc_tr02 {
				gen temp_`x' = `x' if agecat==3
				egen `x'M = max(temp_`x')
				drop temp*
				gen dif_`x' = `x'M-`x'
			}
			keep dif* agecat
			gen finw = 1
		save temp/finw_agecat_2, replace
	restore	

	preserve
		keep if stockinvestor==1 
		collapse (mean) del_ofin_perc_tr02 del_retir_perc_tr02  del_finwealth_perc_tr02, by(agecat) 
			foreach x in del_ofin_perc_tr02 del_retir_perc_tr02 del_finwealth_perc_tr02 {
				gen temp_`x' = `x' if agecat==3
				egen `x'M = max(temp_`x')
				drop temp*
				gen dif_`x' = `x'M-`x'
			}
			keep dif* agecat
			gen stock = 1
		save temp/stock_agecat_2, replace
	restore	
	
	
	use temp/uncond_agecat_2, replace
	append using temp/finw_agecat_2
	append using temp/stock_agecat_2
	gen sample = "uncond" if uncond==1
	replace sample = "finw" if finw==1
	replace sample = "stock" if stock==1
	drop uncond finw stock 
	order age sample
	
	foreach x in dif_del_ofin_perc_tr02 dif_del_retir_perc_tr02 dif_del_finwealth_perc_tr02 {
		replace `x' = round(`x', 0.01)
	}	
	
	sort sample agecat
	
	rename dif_del_ofin_perc_tr02 ofin
	rename dif_del_retir_perc_tr02 retire 
	rename dif_del_finwealth_perc_tr02 total
	
	drop if agecat==3
save temp/cond_age, replace


	
	
* bottom panel
use data/processed/data_1, clear
drop if agecat==.

	* Scale USD variables	
		foreach x in  delofin_USD_tr02 delretir_USD_tr02  delfinwealth_USD_tr02   delhhnetinc_USD_tr02 ///
		delofin_USD_tr02alt delretir_USD_tr02alt  delfinwealth_USD_tr02alt	{
			replace `x' = `x'/1000
		}
		foreach x in ofinwealth_lvl retirewealth_lvl  finwealth_Jan20 hhinc_lvl hhnetinc_2019   {
			replace `x' = `x'/1000
		}
	*
		
		sum riskshare_finwealth_Jan201, d
		gen riskshare_5 = .
		replace riskshare_5=1 if riskshare_finwealth_Jan201>=0 &riskshare_finwealth_Jan201<=20 & riskshare_finwealth_Jan201!=.
		replace riskshare_5=2 if riskshare_finwealth_Jan201>20 &riskshare_finwealth_Jan201<=40 & riskshare_finwealth_Jan201!=.
		replace riskshare_5=3 if riskshare_finwealth_Jan201>40 &riskshare_finwealth_Jan201<=60 & riskshare_finwealth_Jan201!=.
		replace riskshare_5=4 if riskshare_finwealth_Jan201>60 &riskshare_finwealth_Jan201<=80 & riskshare_finwealth_Jan201!=.
		replace riskshare_5=5 if riskshare_finwealth_Jan201>80 &riskshare_finwealth_Jan201<=100 & riskshare_finwealth_Jan201!=.
		tab riskshare_5, m

	keep if stockinvestor==1
	gen base = (agecat==3)

		foreach x in 1 2 4 5 6 {
			reg del_ofin_perc_tr02 base i.riskshare_5 if agecat==`x'| base==1, cluster(responseid)
			local coef_ofin`x' = round(_b[base], 0.01)
			
			reg del_retir_perc_tr02 base i.riskshare_5 if agecat==`x'| base==1, cluster(responseid)
			local coef_ret`x' =round(_b[base], 0.01)
			
			reg del_finwealth_perc_tr02 base i.riskshare_5 if agecat==`x'| base==1, cluster(responseid)
			local coef_total`x' = round(_b[base], 0.01)
		}
* make bottom panel
clear
set obs 6
gen ofin = .
gen retire = .
gen total = .
foreach x in 1 2 4 5 6 {
	replace ofin = `coef_ofin`x'' in `x'
	replace retire = `coef_ret`x'' in `x'
	replace total = `coef_total`x'' in `x'
}

gen agecat = _n
drop if agecat==3
append using temp/cond_age
gsort -sample agecat
replace sample = "cond eq" if sample==""

list

	